package com.bdqn;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class UserServiceTester {

    @Resource
    private UserService userService;

    /**
     * 测试分页查询（通过JpaSpecificationExecutor接口）
     */
    @Test
    public void testFindPageByMap() {
        int pageIndex = 0, pageSize = 2; // 分页要素，页数从 0 开始
        // 控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "usrId");

        Map param = new HashMap();
        param.put("usrName", "老师");
        param.put("roleId", 1L); // 测试一个查询条件，也可不给或给多个

        // 控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
        Page<User> userPage = userService.findPageByMap(param, pageable);

        System.out.println("总记录数: " + userPage.getTotalElements());
        System.out.println("总页数: " + userPage.getTotalPages());
        System.out.println("当前页数: " + (userPage.getNumber() + 1));
        System.out.println("每页记录数: " + userPage.getSize());
        System.out.println("当前页记录数: " + userPage.getNumberOfElements());
        System.out.println("--------------------------------");
        for (User User : userPage.getContent()) { // 获得查询记录
            System.out.println(User.toString());
        }
        System.out.println("--------------------------------");
    }
}
